package com.estar.assistsurvy.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by admin on 2016/9/21.
 */

public abstract  class BaseRVAdapter<N> extends RecyclerView.Adapter<BaseViewHolder> {

    @SuppressWarnings("unused")
	private BaseViewHolder holder = null;

    private BaseViewHolder.OnItemClickListener onItemClickListener;
    private List<N> data;
    private int layoutRid;
    private Context context;

    public abstract void dataBindView(BaseViewHolder holder,N n);

    public BaseRVAdapter(List<N> data, int layoutRid, Context context) {
        this.data = data;
        this.layoutRid = layoutRid;
        this.context = context;
    }

    public void setOnItemClickListener(BaseViewHolder.OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
        notifyDataSetChanged();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return holder = new BaseViewHolder(LayoutInflater.from(context).inflate(layoutRid,null));
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        dataBindView(holder,getData().get(position));
        if(onItemClickListener!=null){
            holder.setOnItemClickListener(onItemClickListener,position);
        }
    }

    public List<N> getData(){
        return data;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}
